<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>日志管理</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../assets/module/dtree/dtree.css"/>
    <link rel="stylesheet" href="../../assets/module/dtree/font/dtreefont.css"/>
    <link rel="stylesheet" href="../../assets/module/admin.css?v=313"/>
    <link rel="stylesheet" href="../../assets/css/publicStyle.css"/>
    <!--[if lt IE 9]>
    <script src="../../assets/libs/html5shiv.min.js"></script>
    <script src="../../assets/libs/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-form-pane .layui-form-label{
            height: 30px;
            padding: 4px 15px;
        }
        .time-input{
            width: 270px!important;
        }
        #page-box{
            float: right;
        }
        #page-box .layui-box{
            margin: 0;
        }
    </style>
    <!-- 正文开始 -->
<body>
<div class="layui-fluid ">
    <div class="layui-card">
        <div class="layui-card-body ">
            <form class="layui-form layui-form-pane" lay-filter="sysLogForm" id="sysLogForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-block">
                            <select name="type" lay-filter="aihao">
                                <option value="0">登陆日志</option>
                                <option value="1">操作日志</option>
                                <option value="2">数据更新日志</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">请选择起止时间</label>
                        <div class="layui-input-inline time-input">
                            <input name="time" type="text" class="layui-input" id="timeinput" placeholder="开始 到 结束">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn layui-btn-sm" lay-filter="sysLogSubmit" lay-submit id="sysLogSubmit">
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                    <div id="page-box"></div>
                </div>

            </form>

            <table class="layui-table" id="tableLog" lay-filter="tableLog"></table>
        </div>
    </div>
</div>

<!-- js部分 -->
<script type="text/javascript" src="../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../assets/js/common.js?v=313"></script>

<script src="../../assets/libs/template-web.js"></script>
<script src="../../assets/libs/axios.min.js"></script>
<script src="../../assets/js/config.js"></script>
<script src="../../assets/js/authority.js"></script>
<script type="text/javascript">
    <!--自定义函数判断权限-->
    var userInfo = getAuthority();
    var powerlist = JSON.parse(userInfo).permissions;
    /*自定义函数判断权限*/
    template.defaults.imports.hasPower = function(data){
        if(powerlist.includes(data)){
            return true;
        }
        return false;
    };

    layui.use(['laydate','form','table','laypage'], function () {
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var form = layui.form;
        var table = layui.table;
        var laypage = layui.laypage;
        var Authorization;//请求接口用

        var current = 1;
        var type = 0;
        var startTime = "";
        var endTime = "";

        laydate.render({
            elem: '#timeinput'
            ,type: 'datetime'
            ,range: '到'
            ,format: 'yyyy-M-d H:m:s'
        });

        //获取权限方法
        if (userInfo) {
            var access_token = localStorage.getExpire("access_token");
            var token_type = localStorage.getExpire("token_type");
            Authorization = token_type + " " + access_token;
            getLogData();
        }

        // 搜索
        form.on('submit(sysLogSubmit)', function (data) {
            type = data.field.type;
            let time = data.field.time;
            if(time.indexOf("到")!=-1){
                let index = time.indexOf("到");
                startTime = time.substr(0,index);
                endTime = time.substr(index+1);
            }else{
                startTime = "";
                endTime = "";
            }
            getLogData();

            return false;
        });

        function getLogData() {
            var req={
                current: current,
                size: pageSize,
                type:type,
                startTime:startTime,
                endTime:endTime
            };
            axios.get(systemUrl.sysLog.getLogByType,{
                params: req,
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        renderTable(data);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        //渲染表格
        function renderTable(data){
            table.render({
                elem: '#tableLog',
                cellMinWidth: 100,
                data:data.records,
                cols: [[
                    {type: 'numbers', title: '#'},
                    {field: 'moduleName', title: '模块名'},
                    {field: 'actionName',  title: '操作名'},
                    {field: 'type',  title: '日志类型',templet:function (d) {
                        switch (d.type){
                            case "0":
                                return "登陆日志";
                                break;
                            case "1":
                                return "操作日志";
                                break;
                            case "2":
                                return "数据更新日志";
                                break;
                            default:
                                return "不能识别";
                                break;
                        }
                    }},
                    {field: 'serviceId',  title: '服务ID'},
                    {field: 'remoteAddr',  title: '操作IP地址'},
                    {field: 'userAgent',  title: '用户代理'},
                    {field: 'requestUri',  title: '请求URI'},
                    {field: 'method',  title: '操作方式'},
//                    {field: 'params',  title: '操作提交的数据',
//                        templet:function (d) {
//                            if(d.params!=null){
//                                let result = '<pre class="layui-code" lay-encode="false"><a href="/detail/" class="layui-table-link">2222222222222222</a></pre> ';
//                                return result;
//                            }else{
//                                return "";
//                            }
//                        }
//                    },

                    {field: 'time',  title: '执行时间'},
                    {field: 'exception',  title: '异常信息'},
                    {field: 'delFlag',  title: '删除标记'},
                    {field: 'createBy',  title: '创建者'},
                    {field: 'createTime',  title: '创建时间',templet:function (d) {
                        let result = formatDate(d.createTime);
                        return result;
                    }},
                    {field: 'updateTime',  title: '更新时间',templet:function(d){
                        if(d.updateTime!=null){
                            let result = formatDate(d.updateTime);
                            return result;
                        }else{
                            return "";
                        }

                    }},
                    {field: 'status',  title: '操作状态',templet:function (d) {
                        switch (d.status){
                            case "0":
                                return "成功";
                                break;
                            case "1":
                                return "失败";
                                break;
                            default:
                                return "不能识别";
                                break;
                        }
                    }},
                ]]
            });

            laypage.render({
                elem: 'page-box'
                ,count: data.total,
                limit:pageSize,
                groups:1,
                current:current,
                curr:current
                ,layout: ['count','prev','page', 'next']
                ,jump: function(obj, first){
                    if(!first){
                        current = obj.curr;
                        getLogData();
                    }
                }
            });

        }

        //时间戳转换方法    date:时间戳数字
        function formatDate(date) {
            var date = new Date(date);
            var YY = date.getFullYear() + '-';
            var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
            var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            return YY + MM + DD +" "+hh + mm + ss;
        }

    })
</script>

</body>

</html>